/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities;

import java.io.Serializable;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.hibernate.annotations.CollectionOfElements;

import entities.vo.ArticuloEnvtVO;
import entities.vo.ENVTVO;

/**
 *
 * @author Jhonny
 */
@Entity
@IdClass(IdENVT.class)
public class ENVT implements Serializable {
	private static final long serialVersionUID = 750976676783499356L;
	
	@Id
	@AttributeOverrides({
	@AttributeOverride(name = "nroEnvt",
	column = @Column(name="nroEnvt")),
	@AttributeOverride(name = "idCentroDist",
	column = @Column(name="idCentroDist"))
	})
	
    private Long nroEnvt;
    private Long idCentroDist;
    private Long idTienda;
    private Long nroPalc;
    
    private long fecha;
    private List<ItemEnvt> articulos;
    private Pedido pedido;

    @ManyToOne(targetEntity=entities.Pedido.class)
    @JoinColumn(nullable=false, name="Pedido_nroPalc")    	
    public Pedido getPedido() {
    	return pedido;
    }

    public void setPedido(Pedido pedido) {
    	this.pedido = pedido;
    }

	public ENVT() {
        articulos = new LinkedList<ItemEnvt>();
    }

    public long getFecha() {
		return fecha;
	}
    public void setFecha(long fecha) {
		this.fecha = fecha;
	}
    @OneToMany(fetch=FetchType.EAGER)
    @CollectionOfElements
    public List<ItemEnvt> getArticulos() {
        return articulos;
    }

    public void setArticulos(List<ItemEnvt> articulos) {
        this.articulos = articulos;
    }

    public Long getIdCentroDist() {
        return idCentroDist;
    }

    public void setIdCentroDist(Long idCentroDist) {
        this.idCentroDist = idCentroDist;
    }

    @Id
    public Long getNroEnvt() {
        return nroEnvt;
    }

    public void setNroEnvt(Long nroEnvt) {
        this.nroEnvt = nroEnvt;
    }

    public void addArticulo(ItemEnvt articulo) {
        articulos.add(articulo);
    }

    
    public Long getIdTienda() {
		return idTienda;
	}

	public void setIdTienda(Long idTienda) {
		this.idTienda = idTienda;
	}

	public Long getNroPalc() {
		return nroPalc;
	}

	public void setNroPalc(Long nroPalc) {
		this.nroPalc = nroPalc;
	}

	public void setENVTVO(ENVTVO envtvo){
    	setIdCentroDist(envtvo.getIdCentroDist());
    	setNroEnvt(envtvo.getNroEnvt());
    	setFecha(Calendar.getInstance().getTimeInMillis());
    	setIdTienda(envtvo.getIdTienda());
    	setNroPalc(envtvo.getNroPalc());
    	ItemEnvt articuloEnvt=null;
    	for (ArticuloEnvtVO articuloEnvtVO : envtvo.getArticulos()) {
    		articuloEnvt = new ItemEnvt();
    		articuloEnvt.setArticuloEnvtVO(articuloEnvtVO);
			addArticulo(articuloEnvt);
		}
    }
    @Transient
    public ENVTVO getENVTVO(){
//    	ENVTVO envtvo = new ENVTVO();
//    	envtvo.setNroEnvt(getNroEnvt());
//    	envtvo.setIdCentroDist(getIdCentroDist());
//    	for (Articulo articulo : getArticulos()) {
//			envtvo.addArticulo(articulo.getArticuloVO());
//		}
    	return null;
    }
}
